
package com.eltonkent.ui.adapters;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.eltonkent.R;
import com.eltonkent.ui.ApplicationConstants;
import com.eltonkent.ui.containers.SafeEntryContainer;
import com.eltonkent.ui.wrappers.SafeEntriesListWrapper;
import com.eltonkent.util.ImageUtil;

public class SafeEntryListAdapter extends ArrayAdapter<SafeEntryContainer>
{
	private final int currentTextSize;

	public SafeEntryListAdapter(SharedPreferences prefs, Context context, int textViewResourceId,
			SafeEntryContainer[] objects)
	{
		super(context, textViewResourceId, objects);
		currentTextSize = prefs.getInt(ApplicationConstants.PREF_TEXT_SIZE,
				ApplicationConstants.PREF_DEF_TEXTSIZE);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View layout = convertView;
		SafeEntriesListWrapper wrapper;
		if(layout == null)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			layout = inflater.inflate(R.layout.listview_icon_dblline_template, parent, false);
			wrapper = new SafeEntriesListWrapper(layout);
			layout.setTag(wrapper);
		} else
		{
			wrapper = (SafeEntriesListWrapper) layout.getTag();
		}
		SafeEntryContainer listData = getItem(position);
		wrapper.getEntryIcon().setImageBitmap(
				ImageUtil.createBitmapFromAsset(getContext(), listData.getIconID()));
		wrapper.getEntryName().setText(listData.getSafeEntryName());
		wrapper.getEntryType().setText(listData.getEntryTemplateName());

		wrapper.getEntryName().setTextSize(currentTextSize);
		wrapper.getEntryType().setTextSize(currentTextSize);

		return layout;
	}
}
